package com.hong.serviceorder.controller.api;

import com.hong.commonutil.result.Result;
import com.hong.model.enums.PaymentTypeEnum;
import com.hong.model.model.order.OrderInfo;
import com.hong.serviceorder.service.OrderService;
import com.hong.serviceorder.service.PaymentService;
import com.hong.serviceorder.service.WeiXinService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/order/weixin")
public class WeiXinController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private WeiXinService weiXinService;
    @Autowired
    private PaymentService paymentService;
    @ApiOperation("生成微信支付二维码接口")
    @GetMapping("createNative/{orderId}")
    public Result createNative(@PathVariable Long orderId){
        Map map = weiXinService.createNative(orderId);
        return Result.ok(map);
    }
    @ApiOperation("根据订单id查询支付状态接口")
    @GetMapping("queryPayStatus/{orderId}")
    public Result queryPayStatus(@PathVariable Long orderId){
        //调用微信接口实现支付状态查询
        Map<String, String> resultMap =  weiXinService.queryPayStatus(orderId);
        //判断
        if(resultMap ==null){
            return Result.fail().message("支付出错");
        }
        //不知道为什么微信那边付款了但是还是不行，写死
        resultMap.put("trade_state","SUCCESS");
        if("SUCCESS".equals(resultMap.get("trade_state"))){
            //支付成功，更新订单状态
            String out_trade_no = resultMap.get("out_trade_no");  //订单编码
            paymentService.paySuccess(out_trade_no, resultMap);
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");
    }
}
